package tool

import (
	"cloudRestaurant/model"
	"src/github.com/go-xorm/xorm"
	_ "src/github.com/go-sql-driver/mysql"
)

var DbEngine *Orm

// 通过操作OrmEngine这个对象来完成数据库的操作
type Orm = struct {
	*xorm.Engine
}

func OrmEngine(cfg *Config) (*Orm, error) {
	db := _cfg.DataBase
	conn := db.User + ":" + db.Password + "@tcp(" + db.Host + ":" + db.Port + ")/" + db.DbName + "?charset=" + db.Charset
	engine, err := xorm.NewEngine(db.Driver, conn)
	if err != nil {
		return nil, err
	}
	//表示是否输出sql语句
	engine.ShowSQL(db.ShowSql)

	err = engine.Sync2(new(model.SmsCode),new(model.Member))
	if err != nil {
		return nil, err
	}
	orm := new(Orm)
	orm.Engine = engine
	DbEngine = orm
	return orm, nil
}
